package 华为OD机试真题2023.进阶题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import utils.MyUtil;

public class HJ04_Excel单元格数值统计 {
  public static void main(String[] args) throws FileNotFoundException {
    MyUtil.START();
    //    Solution();
    Solution2();
    MyUtil.END();
  }

  static String[][] matrix;
  static Map<Character, Integer> index =
      new HashMap<Character, Integer>() {
        {
          put('A', 0);
          put('B', 1);
          put('C', 2);
          put('D', 3);
          put('E', 4);
          put('F', 5);
          put('G', 6);
          put('H', 7);
          put('I', 8);
          put('J', 9);
          put('K', 10);
          put('L', 11);
          put('M', 12);
          put('N', 13);
          put('O', 14);
          put('P', 15);
          put('Q', 16);
          put('R', 17);
          put('S', 18);
          put('T', 19);
          put('U', 20);
          put('V', 21);
          put('W', 22);
          put('X', 23);
          put('Y', 24);
          put('Z', 25);
        }
      };

  static void Solution() throws FileNotFoundException {
    Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/04.txt"));
    int row = in.nextInt();
    int column = in.nextInt();
    in.nextLine(); // 吸收回车
    matrix = new String[row][column];
    for (int i = 0; i < row; i++) {
      String[] tmp = in.nextLine().split(" ");
      matrix[i] = tmp;
    }
    String[] query = in.nextLine().split(":");
    int qRow1 = Integer.parseInt(query[0].substring(1)) - 1;
    int qRow2 = Integer.parseInt(query[1].substring(1)) - 1;
    int qCol1 = index.get(query[0].charAt(0));
    int qCol2 = index.get(query[1].charAt(0));
    int res = 0;
    for (int i = qRow1; i <= qRow2; i++) {
      for (int j = qCol1; j <= qCol2; j++) {
        res += getNum(i, j);
      }
    }
    System.out.println(res);
  }

  static int getNum(int x, int y) {
    String res = matrix[x][y];
    if (res.contains("=")) {
      if (res.contains("+")) {
        String[] shizi = res.split("\\+");
        int a1 = trueNum(shizi[0].substring(1));
        int a2 = trueNum(shizi[1]);
        return a1 + a2;
      } else if (res.contains("-")) {
        String[] shizi = res.split("\\-");
        int a1 = trueNum(shizi[0].substring(1));
        int a2 = trueNum(shizi[1]);
        return a1 - a2;
      } else {
        int row = Integer.parseInt(res.substring(2)) - 1;
        int col = index.get(res.charAt(1));
        return getNum(row, col);
      }
    } else {
      return Integer.parseInt(res);
    }
  }

  static int trueNum(String s) {
    if (Character.isAlphabetic(s.charAt(0))) {
      int row = Integer.parseInt(s.substring(1)) - 1;
      int col = index.get(s.charAt(0));
      return getNum(row, col);
    } else {
      return Integer.parseInt(s);
    }
  }

  static void Solution2() throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/04.txt"));
    int rows;
    int cols;
    String[][] items;

    int startX, startY, endX, endY;
    int sum = 0;

    rows = scanner.nextInt();
    cols = scanner.nextInt();
    scanner.nextLine();
    items = new String[rows][cols];
    for (int i = 0; i < rows; i++) {
      String line = scanner.nextLine();
      String[] content = line.split("[\\t ]+");
      for (int j = 0; j < cols; j++) {
        if (j < content.length) {
          items[i][j] = content[j];
        }
      }
    }
    String line = scanner.nextLine();
    String[] sel = line.split(":");
    startY = sel[0].charAt(0) - 'A';
    startX = Integer.parseInt(sel[0].substring(1)) - 1;
    endY = sel[1].charAt(0) - 'A';
    endX = Integer.parseInt(sel[1].substring(1)) - 1;

    for (int i = startX; i <= endX; i++) {
      for (int j = startY; j <= endY; j++) {
        sum += evaluate(items, i, j);
      }
    }
    System.out.println(sum);
  }

  static int evaluate(String[][] items, int i, int j) {
    String s = items[i][j];
    if (s.startsWith("=")) {
      int res;
      s = s.substring(1);
      int indexPlus = s.indexOf("+");
      if (indexPlus > 0) {
        String s1 = s.substring(0, indexPlus);
        String s2 = s.substring(indexPlus + 1);
        int n1 = getNoValue(items, s1);
        int n2 = getNoValue(items, s2);
        res = n1 + n2;
      } else {
        int indexMinus = s.indexOf("-");
        if (indexMinus > 0) {
          String s1 = s.substring(0, indexMinus);
          String s2 = s.substring(indexMinus + 1);
          int n1 = getNoValue(items, s1);
          int n2 = getNoValue(items, s2);
          res = n1 - n2;
        } else {
          res = getNoValue(items, s);
        }
      }
      items[i][j] = String.valueOf(res);
      return res;

    } else {
      return Integer.parseInt(s);
    }
  }

  static int getNoValue(String[][] items, String no) {
    int n;
    if (Character.isDigit(no.charAt(0))) {
      n = Integer.parseInt(no);
    } else {
      int[] p1 = excelPos(no);
      n = evaluate(items, p1[0], p1[1]);
    }
    return n;
  }

  /**
   * @param s excel单元格编号
   * @return 对应数组下标[x, y]
   */
  static int[] excelPos(String s) {
    int[] pos = new int[2];
    pos[0] = Integer.parseInt(s.substring(1)) - 1;
    pos[1] = s.charAt(0) - 'A';
    return pos;
  }
}
